<template>
  <view class="sort-page">
    <!-- 顶部标题栏 -->
    <view class="fixed-bar" :style="{ paddingTop: (safeAreaInsets?.top ?? 0) + 10 + 'px' }">
      <view class="header">
        <img src="@/static/images/back_icon.png" class="back-icon" @click="backFn()" />
        <text class="title">临期物品</text>
        <img src="" class="back-icon" />
      </view>
      <view class="tabs">
        <view
          v-for="(tab, idx) in tabs"
          :key="tab"
          :class="['tab', { active: idx === activeTab }]"
          @tap="activeTab = idx"
          >{{ tab }}</view
        >
      </view>
    </view>
    <!-- 临期物品列表 -->
    <view class="item-list">
      <view class="item" v-for="item in filteredList" :key="item.id">
        <view class="item-top">
          <view class="expire-info">
            <text class="expire-label">到期日期：</text>
            <text class="expire-date">{{ item.expireDate }}</text>
          </view>
          <view class="expire-days">{{ item.days }} 天后过期</view>
        </view>
        <view class="item-content">
          <image :src="item.img" class="item-img" />
          <view class="item-info">
            <view class="item-name">{{ item.name }}</view>
            <view class="item-desc">{{ item.position }}</view>
          </view>
          <view class="item-count">{{ item.count }}瓶</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

const tabs = ['全部', '酒类', '日用品', '化妆品', '药品', '零食']
const activeTab = ref(0)
const { safeAreaInsets } = uni.getSystemInfoSync()

const list = [
  {
    id: 1,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 2,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 2,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 5,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 3,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 7,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 4,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 12,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
  {
    id: 5,
    name: '维生素橘子味水果软糖',
    img: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1667112472.92265090.jpg',
    expireDate: '2025.05.26',
    days: 16,
    position: '客厅/零食柜/第一层',
    count: 2,
    type: '零食',
  },
]

const filteredList = computed(() => {
  if (activeTab.value === 0) return list
  return list.filter((item) => item.type === tabs[activeTab.value])
})
const backFn = () => {
  uni.navigateBack()
}
</script>

<style scoped>
.fixed-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: #fff;
}
.back-icon {
  width: 36rpx;
  height: 36rpx;
  margin-left: 32rpx;
}
.sort-page {
  background: #f7f8fa;
  min-height: 100vh;
  padding-bottom: 20rpx;
}
.header {
  padding: 32rpx 0 16rpx 0;
  font-weight: bold;
  font-size: 36rpx;
  background: #fff;
  display: flex;
  justify-content: space-between;
}
.tabs {
  display: flex;
  background: #f7f8fa;
  padding: 0 24rpx;
  border-bottom: 1rpx solid #eee;
}
.tab {
  flex: 1;
  text-align: center;
  padding: 24rpx 0;
  font-size: 28rpx;
  color: #888;
  font-weight: 500;
  position: relative;
}
.tab.active {
  color: #2979ff;
  font-weight: bold;
}
.tab.active::after {
  content: '';
  display: block;
  width: 60rpx;
  height: 6rpx;
  background: #2979ff;
  border-radius: 3rpx;
  margin: 0 auto;
  margin-top: 8rpx;
}
.item-list {
  padding: 24rpx;
}
.item {
  background: #fff;
  border-radius: 16rpx;
  margin-bottom: 28rpx;
  padding: 0 0 20rpx 0;
  box-shadow: 0 2rpx 8rpx #0001;
}
.item-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff5ec;
  border-radius: 16rpx 16rpx 0 0;
  padding: 0 20rpx;
  height: 48rpx;
}
.expire-info {
  display: flex;
  align-items: center;
}
.expire-label {
  color: #ff4d4f;
  font-size: 24rpx;
}
.expire-date {
  color: #ff4d4f;
  font-size: 24rpx;
  margin-left: 4rpx;
}
.expire-days {
  color: #ff7d00;
  font-size: 24rpx;
  font-weight: bold;
}
.item-content {
  display: flex;
  align-items: center;
  padding: 20rpx;
}
.item-img {
  width: 80rpx;
  height: 80rpx;
  border-radius: 12rpx;
  object-fit: cover;
  margin-right: 20rpx;
}
.item-info {
  flex: 1;
}
.item-name {
  font-size: 30rpx;
  font-weight: bold;
  margin-bottom: 6rpx;
}
.item-desc {
  color: #888;
  font-size: 24rpx;
  margin-bottom: 6rpx;
}
.item-count {
  color: #888;
  font-size: 26rpx;
  margin-left: 16rpx;
  white-space: nowrap;
}
</style>
